package com.lingchou.cloud.process.client.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lingchou.cloud.process.api.dto.constant.FlowConstant;
import com.lingchou.cloud.process.api.dto.param.CommentVo;
import com.lingchou.cloud.process.api.dto.result.base.TaskRecordInfo;
import com.lingchou.cloud.process.api.enums.CommentTypeEnum;
import com.lingchou.cloud.process.client.cmd.AddHisCommentCmd;
import com.lingchou.cloud.process.client.dao.FlowAbleCommentDao;
import com.lingchou.cloud.process.client.service.FlowAbleCommentService;
import org.flowable.engine.ManagementService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : bruce.liu
 * @title: : FlowCommentServiceImpl
 * @projectName : flowable
 * @description: 流程备注service
 * @date : 2019/11/2412:58
 */
@Service
public class FlowAbleCommentServiceImpl implements FlowAbleCommentService {

    private final ManagementService managementService;
    private final FlowAbleCommentDao flowableCommentDao;

    public FlowAbleCommentServiceImpl(ManagementService managementService, FlowAbleCommentDao flowableCommentDao) {
        this.managementService = managementService;
        this.flowableCommentDao = flowableCommentDao;
    }


    @Override
    public void addComment(CommentVo comment) {
        managementService.executeCommand(new AddHisCommentCmd(comment.getTaskId(), comment.getUserId(), comment.getProcessInstanceId(),
                comment.getType(), comment.getMessage()));
    }

    @Override
    public void addComment(String taskId, String operator, String processInstanceId, String type, String comment) {
        managementService.executeCommand(new AddHisCommentCmd(taskId, operator, processInstanceId,
                type, comment));
    }

    @Override
    public List<CommentVo> getFlowCommentVosByProcessInstanceId(String processInstanceId) {
        List<CommentVo> data = flowableCommentDao.getFlowCommentVosByProcessInstanceId(processInstanceId);
        data.forEach(commentVo -> commentVo.setTypeName(CommentTypeEnum.getEnumMsgByType(commentVo.getType())));
        return data;
    }

    @Override
    public List<TaskRecordInfo> getRecord(String processInstanceId) {
        List<TaskRecordInfo> taskRecordInfos = flowableCommentDao.getTaskRecord(processInstanceId);
        taskRecordInfos.forEach(taskRecordInfo -> {
            //删除条件为空则为正在审批或者是
            if (StrUtil.isBlank(taskRecordInfo.getDeleteReason())) {
                taskRecordInfo.setSignType(taskRecordInfo.getEndTime() == null ?
                        CommentTypeEnum.IN_REVIEW.toString() : taskRecordInfo.getSignType());
            }
            if (StrUtil.isNotBlank(taskRecordInfo.getDeleteReason()) &&
                    taskRecordInfo.getDeleteReason().contains("boundary event")) {
                taskRecordInfo.setSignType(CommentTypeEnum.TIME_OUT.toString());
                taskRecordInfo.setSignMessage(FlowConstant.TIME_OUT_STR);
                taskRecordInfo.setSignTime(taskRecordInfo.getEndTime());
            }
            if (StrUtil.isNotBlank(taskRecordInfo.getDeleteReason()) &&
                    taskRecordInfo.getDeleteReason().contains(FlowConstant.REVOKE_REASON)) {
                taskRecordInfo.setSignType(CommentTypeEnum.REVOKE.toString());
                taskRecordInfo.setSignMessage(FlowConstant.REVOKE_STR);
                taskRecordInfo.setSignTime(taskRecordInfo.getEndTime());
            }

        });
        return taskRecordInfos;
    }

}
